using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class RollWindowClass : MonoBehaviour {
	
	public UILabel rollTypeHeader;
	public UILabel bonusHeader;
	public UILabel RollingLabel;
	public GameObject OKButton;
	public GameObject AgainButton;
	public UILabel LastRoll;
	
	string type;
	List<RollDefinition> rolls;
	
	public int rawDieResult;
	
	List<List<RollDefinition>> rollsSeries;
	public WeaponItem weapon;
	
	bool rolling = false;
	bool critConfirming = false;

	// Use this for initialization
	void Start () {
		UIButtonMessage message = OKButton.gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = rollsSeries != null?"DamageClicked":"OKClicked";
		
		message = AgainButton.gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = rollsSeries != null?"NextClicked":"AgainClicked";
		
		NGUITools.AddWidgetCollider(RollingLabel.gameObject);
		
		RollingLabel.text = "-";
		message = RollingLabel.gameObject.AddComponent<UIButtonMessage>();
		message.target = gameObject;
		message.functionName = rollsSeries != null?"NextClicked":"AgainClicked";
		
		LastRoll.text = "";
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	public IEnumerator RollSeries(List<List<RollDefinition>> rolls, string rolltype, WeaponItem weapon)
	{
		this.weapon = weapon;
		
		AgainButton.GetComponentInChildren<UILabel>().text = "Next ->";
		
		rollsSeries = rolls;
		OKButton.GetComponentInChildren<UILabel>().text = "Damage";
		
		StartCoroutine(Roll(rolls[0], rolltype));
		rollsSeries.RemoveAt(0);
		
		if(rollsSeries.Count == 0)
		{
			AgainButton.GetComponentInChildren<UILabel>().text = "Done";
		}
		
		yield break;
	}
	
	public IEnumerator Roll(List<RollDefinition> rolls, string rolltype)
	{
		if(!rolling)
		{
			rollTypeHeader.text = rollTypeHeader.text.Replace("(object)", rolltype);
			
			type = rolltype;
			this.rolls = rolls;
			rolling = true;
			bonusHeader.text = rolls.Description() + " =";
			
			int i = 0;
			while(i < 10)
			{
				if(RollingLabel.text == "Rolling")
					RollingLabel.text = "Rolling.";
				else if(RollingLabel.text == "Rolling.")
					RollingLabel.text = "Rolling..";
				else if(RollingLabel.text == "Rolling..")
					RollingLabel.text = "Rolling...";
				else
					RollingLabel.text = "Rolling";
				i++;
				yield return new WaitForSeconds(0.1f);
			}
			
			RollingLabel.text = rolls.Roll(this);
			
			if(rollsSeries != null)
			{
				if(rawDieResult >= weapon.critBounds)
				{
					critConfirming = true;
					OKButton.GetComponentInChildren<UILabel>().text = "Confirm?";
				}
			}
			
			rolling = false;
		}
		
		yield break;
	}
	
	IEnumerator AgainClicked()
	{
		int i = RollingLabel.text.IndexOf(":");
		LastRoll.text = "Last Result: " + ((i > 0)?RollingLabel.text.Substring(0, i):RollingLabel.text);
		yield return StartCoroutine(Roll(rolls, type));
	}
	
	IEnumerator NextClicked()
	{
		if(rollsSeries.Count > 0 && !rolling)
		{
			if(critConfirming == true)
			{
				critConfirming = false;
				OKButton.GetComponentInChildren<UILabel>().text = "Damage";
			}
			int i = RollingLabel.text.IndexOf(":");
			
			if(LastRoll.text == "")
			{
				LastRoll.text = "Prior Results: ";				
				LastRoll.text += ((i > 0)?RollingLabel.text.Substring(0, i):RollingLabel.text);
			}
			else
				LastRoll.text += ", " + ((i > 0)?RollingLabel.text.Substring(0, i):RollingLabel.text);
			
			StartCoroutine(Roll(rollsSeries[0], type));
			rollsSeries.RemoveAt(0);
			
			if(rollsSeries.Count == 0)
			{
				AgainButton.GetComponentInChildren<UILabel>().text = "Done";
			}
		}
		else if(rollsSeries.Count == 0)
		{
			OKClicked();
		}
		
		yield break;
	}
	
	void OKClicked()
	{
		Destroy(gameObject);
	}
	
	void DamageClicked()
	{
		if(!critConfirming)
		{
			List<string> options = new List<string>{"Damage","Critical Damage"};
			
			if(SheetManager.activeCharacter.sneakDice > 0)
			{
				options.Add("Sneak Damage");
				options.Add("Crit. Sneak Damage");
			}
			
			List<SetterWindowDescription> descVar = new List<SetterWindowDescription>
			{
				new SetterWindowDescription("", "Action", false, options)
			};
			
			GameObject outWindow;
			
			HelperFunctions.ButtonWindow("What Kind of Roll?", descVar, "RollType", gameObject, out outWindow);
		}
		else
		{
			critConfirming = false;
			OKButton.GetComponentInChildren<UILabel>().text = "Damage";
			
			int i = RollingLabel.text.IndexOf(":");
			
			if(LastRoll.text == "")
			{
				LastRoll.text = "Prior Results: ";				
				LastRoll.text += ((i > 0)?RollingLabel.text.Substring(0, i):RollingLabel.text);
			}
			else
				LastRoll.text += ", " + ((i > 0)?RollingLabel.text.Substring(0, i):RollingLabel.text);
			
			StartCoroutine(Roll(rolls, type));
		}
	}
	
	void RollType(string type)
	{
		switch(type)
		{
		case "Damage":
			weapon.RollDamage();
			break;
		case "Critical Damage":
			weapon.RollDamage(true);
			break;
		case "Sneak Damage":
			weapon.RollDamage(false,true);
			break;
		case "Crit. Sneak Damage":
			weapon.RollDamage(true, true);
			break;
		}
	}
}
